package com.ycrlkj.android.modulescore.main.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.SPUtils;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.ycrlkj.android.modulescore.main.BR;
import com.ycrlkj.android.modulescore.main.R;
import com.ycrlkj.android.modulescore.main.apiservice.FreightOrderApiService;
import com.ycrlkj.android.modulescore.main.databinding.FragmentFreightOrderListBinding;
import com.ycrlkj.android.modulescore.main.entity.FreightOrder;
import com.ycrlkj.android.modulescore.main.entity.Query.FreightOrderQuery;
import com.ycrlkj.android.modulespublic.common.base.DataBindingRecyclerViewAdapter;
import com.ycrlkj.android.modulespublic.common.constant.RoutePath;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiException;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiExceptionHandler;
import com.ycrlkj.android.modulespublic.common.net.exception.ErrorConsumer;
import com.ycrlkj.android.modulespublic.common.net.transformer.ResponseTransformer;
import com.ycrlkj.android.modulespublic.common.utils.xpopup.XPopupUtil;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class FreightOrderListFragment extends Fragment {

    private int orderStatus; //订单状态: 1=>未接单, 2=>已接单, 3=>已完成, 4=>已取消

    private FragmentFreightOrderListBinding binding;

    @Inject
    FreightOrderApiService freightOrderApiService;

    private RecyclerView recyclerView;

    private DataBindingRecyclerViewAdapter<FreightOrder> adapter;

    private LoadingPopupView loadingPopupView;

    private FreightOrderTabsActivity activity;

    public FreightOrderListFragment() {
    }

    public static FreightOrderListFragment newInstance(int orderStatus) {
        FreightOrderListFragment fragment = new FreightOrderListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("orderStatus", orderStatus);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            this.orderStatus = getArguments().getInt("orderStatus");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        activity = (FreightOrderTabsActivity) getActivity();

        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_freight_order_list, container, false);

        initAdapter();

        getAll();

        return binding.getRoot();
    }

    public void initAdapter() {
        recyclerView = binding.recyclerView;
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        adapter = new DataBindingRecyclerViewAdapter<>(activity, R.layout.item_freight_order, BR.freightOrder, R.id.btn_toDetail, new DataBindingRecyclerViewAdapter.OnItemChildClickListener<FreightOrder>() {
            @Override
            public void onItemChildClickListener(FreightOrder item, int position) {
                String id = item.getId();
                ARouter.getInstance().build(RoutePath.PAGE_FREIGHT_ORDER_DETAIL).withString("id", id).navigation();
            }
        });
        recyclerView.setAdapter(adapter);
    }

    @SuppressLint("CheckResult")
    public void getAll() {
        loadingPopupView = XPopupUtil.showLoading(activity);
        XPopupUtil.hideLoadingDelay(loadingPopupView);

        FreightOrderQuery freightOrderQuery = new FreightOrderQuery();
        freightOrderQuery.setOrderStatus(orderStatus);

        String userId = SPStaticUtils.getString("userId");
        freightOrderQuery.setDriverId(userId);

        freightOrderApiService.getAll(freightOrderQuery)
                .compose(ResponseTransformer.obtain())
                .subscribe(list -> {
                            adapter.add(list);
                            XPopupUtil.hideLoading(loadingPopupView);
                        },
                        new ErrorConsumer() {
                            @Override
                            protected void error(ApiException e) {
                                XPopupUtil.hideLoading(loadingPopupView);
                                ApiExceptionHandler.doWith(activity, e);
                            }
                        });

    }

}